package com.controller;

import com.bean.Provider;
import com.service.ProviderService;
import com.util.OrderAssistant;
import com.util.PageAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping("query")
    public String query(HttpSession session, com.vo.Provider providerQueryInfo,
                        PageAssistant<Provider> paProvider, OrderAssistant order){
        System.out.println(">>> provider query");
        System.out.println(providerQueryInfo);
        System.out.println(paProvider);
        System.out.println(order);
        System.out.println("-----------------------------------------------");
        //创建参数集合
        Map<String, Object> map = new HashMap<>();
        //添加查询参数
        map.put("idName", providerQueryInfo.getIdName());
        map.put("key", providerQueryInfo.getProviderName());
        //设置排序参数
        map.put("order", order.sql());
        //查询总记录数
        paProvider.setCount(providerService.count(map));
        //设置分页参数
        map.put("start", paProvider.getStart());
        map.put("size", paProvider.getSize());
        //查询当前页内容
        paProvider.setItems(providerService.query(map));
        //保存
        session.setAttribute("paProvider", paProvider);
        session.setAttribute("providerQueryInfo", providerQueryInfo.parse());
        session.setAttribute("orderInfo", order);
        return "/page/provider/providerList.jsp";
    }

    @RequestMapping("unique")
    public String unique(HttpSession session, com.vo.Provider provider, String page){
        System.out.println(">>> provider unique");
        System.out.println(provider);
        System.out.println(page);
        System.out.println("-------------------------------------------------");

        //查询
        List<Provider> providerList = providerService.find(provider.parse());
        //保存
        session.setAttribute("providerInfo", providerList.get(0));

        System.out.println(providerList.get(0));

        //判断, page是否为null
        if (page == null){
            page = "providerDetail.jsp";
        }
        return "/page/provider/" + page;

    }

    @RequestMapping("remove")
    public String remove(HttpSession session, Provider provider){
        System.out.println(">>> provider remove");
        System.out.println(provider);
        System.out.println("-----------------------------------------------");

        //删除
        int count = providerService.remove(provider);

        //保存
        String updateMsg = "删除成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "删除失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);
        return "query";
    }
    @RequestMapping("modify")
    public String modify(HttpSession session, com.vo.Provider provider){
        System.out.println(">>> provider modify");
        System.out.println(provider);
        System.out.println("-----------------------------------------------");

        //修改
        int count = providerService.modify(provider.parse());

        //保存
        String updateMsg = "修改成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "修改失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "/provider/unique";
    }

    @RequestMapping("save")
    public String save(HttpSession session, com.vo.Provider provider){
        System.out.println(">>> provider save");
        System.out.println(provider);
        System.out.println("------------------------------------------------");

        //添加
        int count = providerService.save(provider.parse());

        //保存
        String updateMsg = "添加成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "添加失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "unique";
    }

    public String test(){
        return "";
    }
}
